package com.easylinkin.linkappapi.facerecognition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.facerecognition.entity.FaceCamera;
import com.easylinkin.linkappapi.facerecognition.mapper.FaceCameraMapper;
import com.easylinkin.linkappapi.facerecognition.service.FaceCameraService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FaceCameraServiceImpl extends ServiceImpl<FaceCameraMapper, FaceCamera> implements FaceCameraService {

    @Resource
    LinkappUserContextProducer linkappUserContextProducer;

    @Override
    public IPage<FaceCamera> queryFaceCameras(Page<FaceCamera> page, FaceCamera faceCamera) {
        faceCamera.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        return baseMapper.getFaceCameras(page, faceCamera);
    }

    @Override
    public FaceCamera queryCameraWithPosition(String position) {
        QueryWrapper<FaceCamera> qw = new QueryWrapper<>();
        qw.eq("tenant_id", linkappUserContextProducer.getNotNullCurrent().getTenantId());
        qw.eq("position", position);

        return baseMapper.selectOne(qw);
    }

    @Override
    public FaceCamera queryCameraWithDeviceCode(String deviceCode) {
        QueryWrapper<FaceCamera> qw = new QueryWrapper<>();
        qw.eq("tenant_id", linkappUserContextProducer.getNotNullCurrent().getTenantId());
        qw.eq("device_code", deviceCode);

        return baseMapper.selectOne(qw);
    }

    @Override
    public FaceCamera queryCameraWithDeviceCodeWithoutTenantId(String deviceCode) {
        QueryWrapper<FaceCamera> qw = new QueryWrapper<>();
        qw.eq("device_code", deviceCode);

        return baseMapper.selectOne(qw);
    }

    @Override
    public List<FaceCamera> queryAllFaceCamera() {
        QueryWrapper<FaceCamera> qw = new QueryWrapper<>();
        qw.eq("tenant_id", linkappUserContextProducer.getNotNullCurrent().getTenantId());

        return baseMapper.selectList(qw);
    }
}
